/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package midi;

import music.Note;
import music.types.NoteTypes;

/**
 *
 * @author TJ
 */
public class Metronome{
    private int bpm=120;
    private NoteTypes n=NoteTypes.QUARTER;
    private boolean stopped=false;
    private int selectedNote=42;
    private Thread t;
    private static Metronome met;
    private static String[] percNotes;
    static{
        percNotes=new String[47];
    percNotes[0]="Bass Drum 2";
    percNotes[1]="Bass Drum 1";
    percNotes[2]="Side Stick/Rimshot";
    percNotes[3]="Snare Drum 1";
    percNotes[4]="Hand Clap";
    percNotes[5]="Snare Drum 2";
    percNotes[6]="Low Tom 2";
    percNotes[7]="Closed Hi-hat";
    percNotes[8]="Low Tom 1";
    percNotes[9]="Pedal Hi-hat";
    percNotes[10]="Mid Tom 2";
    percNotes[11]="Open Hi-hat";
    percNotes[12]="Mid Tom 1";
    percNotes[13]="High Tom 2";
    percNotes[14]="Crash Cymbal 1";
    percNotes[15]="High Tom 1";
    percNotes[16]="Ride Cymbal 1";
    percNotes[17]="Chinese Cymbal";
    percNotes[18]="Ride Bell";
    percNotes[19]="Tambourine";
    percNotes[20]="Splash Cymbal";
    percNotes[21]="Cowbell";
    percNotes[22]="Crash Cymbal 2";
    percNotes[23]="Vibra Slap";
    percNotes[24]="Ride Cymbal 2";
    percNotes[25]="High Bongo";
    percNotes[26]="Low Bongo";
    percNotes[27]="Mute High Conga";
    percNotes[28]="Open High Conga";
    percNotes[29]="Low Conga";
    percNotes[30]="High Timbale";
    percNotes[31]="Low Timbale";
    percNotes[32]="High Agogô";
    percNotes[33]="Low Agogô";
    percNotes[34]="Cabasa";
    percNotes[35]="Maracas";
    percNotes[36]="Short Whistle";
    percNotes[37]="Long Whistle";
    percNotes[38]="Short Güiro";
    percNotes[39]="Long Güiro";
    percNotes[40]="Claves";
    percNotes[41]="High Wood Block";
    percNotes[42]="Low Wood Block";
    percNotes[43]="Mute Cuíca";
    percNotes[44]="Open Cuíca";
    percNotes[45]="Mute Triangle";
    percNotes[46]="Open Triangle";
    }
    private Metronome() {}
    public static Metronome getMetronome() {
        if(met==null) {met=new Metronome();}
        return met;
    }
    public void stop() {stopped=true;}
    public void start() {
        if(!stopped && t!=null) {return;}
        t=new Thread(new MetronomeThread());
        t.start();
    }
    public int getSpeed() {
        return bpm;
    }
    private long calculateSleepTime() {
        return 60000/bpm;
    }
    public void setBPM(int b){bpm=b;}
    public int getBPM(){return bpm;}
    public void         setNoteType(NoteTypes t)    {n=t;}
    public NoteTypes    getNoteType()               {return n;}
    public static String[] getPercussionNames(){return percNotes;}
    public static String getPercussionNoteName(int note){
        note-=35;
        if(note<0){note=0;}
        if(note>=percNotes.length) {note=percNotes.length-1;}
        return percNotes[note];
    }
    class MetronomeThread implements Runnable{

        @Override
        public void run() {
            stopped=false;
            MidiPlayer mp=MidiPlayer.getMidiPlayer();
            long elapsed;
            while(!stopped) {
                elapsed=System.currentTimeMillis();
                mp.playNote(9,selectedNote+35,120);
                elapsed=System.currentTimeMillis()-elapsed;
                try{
                    Thread.sleep(calculateSleepTime()-elapsed);
                }catch(Exception e){}
            }
            t=null;
        }
    }
}
